﻿#include "FITKAbaqusAdaptorInteractionPropertySurfaceContact.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConstraintCoupling.h" 
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaConstraintTie.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractInteraction.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionPropertyManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionPropertyContact.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionContactTangentialBehavior.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionContactNormalBehavior.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionContactBehavior.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionContactGeometricProperty.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaInteractionSurfToSurfExplicit.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaStepManager.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractStep.h"
#include <QDebug>

//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorInteractionContactTangentialBehavior::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorInteractionContactTangentialBehavior";
    }

    void FITKAbaqusAdaptorInteractionContactTangentialBehavior::setExplicitStandard(int isexplicit, int isstandard)
    {
        _isExplicit = isexplicit;
        _isStandard = isstandard;
    }

    bool FITKAbaqusAdaptorInteractionContactTangentialBehavior::adaptR()
    {
        // 读取
        _iteractionPropContact = dynamic_cast<Interface::FITKAbaInteractionPropertyContact*>(_dataObj);
        if (!_iteractionPropContact || !_reader) return false;
        //类型
        Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType behaviorType =
            Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::TangentialBehavior;
        bool readOK = false;
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*friction")) return false;
        //创建数据并获取数据
        _iteractionPropContact->addBehavior(behaviorType);
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        //场变量个数
        if (s.hasKey("dependencies"))
        {
            this->_dependencies = s["dependencies"].trimmed().toInt(&readOK);
            if (!readOK)return false;
        }
        //设置摩擦指数衰减类型
        if (s.hasKey("exponential decay"))
        {

        }
        //设置粗糙类型
        else if (s.hasKey("rough"))
        {

        }
        //设置拉格朗日乘子类型
        else if (s.hasKey("lagrange"))
        {

        }
        //设置用户定义类型
        else if (s.hasKey("user"))
        {

        }
        //设置罚类型
        else
        {
            readOK = this->readContactTangentialBehaviorToPenalty();
        }
        if (!readOK)
            _iteractionPropContact->removeBehavior(behaviorType);
        return readOK;
    }

    bool FITKAbaqusAdaptorInteractionContactTangentialBehavior::adaptW()
    {
        // 读取
        _iteractionPropContact = dynamic_cast<Interface::FITKAbaInteractionPropertyContact*>(_dataObj);
        if (!_iteractionPropContact || !_writer) return false;
        //获取切向属性行为
        Interface::FITKAbaInteractionContactTangentialBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactTangentialBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::TangentialBehavior));
        if (!behavior)return true;
        bool writeOK = false;
        //获取摩擦公式类型
        Interface::FITKAbaInteractionContactBehaviorEnum::FrictionFormulation type = behavior->getFrictionFormulation();
        //写出无摩擦
        if (type == Interface::FITKAbaInteractionContactBehaviorEnum::FrictionFormulation::Frictionless)
            writeOK = this->writeContactTangentialBehaviorToFrictionless();
        //写出罚类型
        else if (type == Interface::FITKAbaInteractionContactBehaviorEnum::FrictionFormulation::Penalty)
            writeOK = this->writeContactTangentialBehaviorToPenalty();
        return writeOK;
    }

    bool FITKAbaqusAdaptorInteractionContactTangentialBehavior::readContactTangentialBehaviorToPenalty()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //获取切向属性行为
        Interface::FITKAbaInteractionContactTangentialBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactTangentialBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::TangentialBehavior));
        if (!behavior)return false;
        //设置罚类型
        behavior->setFrictionFormulation(Interface::FITKAbaInteractionContactBehaviorEnum::FrictionFormulation::Penalty);
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        bool ok = false;
        //记录是否是各项异性
        int flagAnisotropic = 1;
        if (s.hasKey("anisotropic"))
        {
            flagAnisotropic = 2;
            behavior->setFrictionDirectionality(Interface::FrictionDiretionalityEnum::FrictionDiretionality::Anisotropic);
        }
        //读取弹性滑动表面尺寸百分比，默认0.005
        if (s.hasKey("slip tolerance"))
        {
            s["slip tolerance"].toDouble(&ok);
            if (!ok)return false;
        }
        //文件应有的一次数据所具有的行数
        int row = (flagAnisotropic + 3 + _dependencies) % 8 ? (flagAnisotropic + 3 + _dependencies) / 8 + 1 : (flagAnisotropic + 3 + _dependencies) / 8;
        //循环读入数据
        int  num = 0;
        int index = 0;
        while (!_reader->atEnd())
        {
            line = _reader->readLine().trimmed();
            if (line.startsWith("*"))break;
            behavior->setRowCount(index + 1);
            //移除最后一个,预防干扰
            if (line.endsWith(','))line.chop(1);
            QStringList str = line.trimmed().split(',');
            for (int j = 0; j < 8 && j < str.size(); ++j)
            {
                ++num;
                //滑移速率
                if (num == flagAnisotropic + 1)
                    if (str.at(j).trimmed().isEmpty())
                        continue;
                    else
                        behavior->setUseSlipRateDependentData(true);
                //接触压力
                if (num == flagAnisotropic + 2)
                    if (str.at(j).trimmed().isEmpty())
                        continue;
                    else
                        behavior->setUseContactPressureDependentData(true);
                //温度
                if (num == flagAnisotropic + 3)
                    if (str.at(j).trimmed().isEmpty())
                        continue;
                    else
                        behavior->setUseTemperatureDependentData(true);
                //读取数据
                double v = str.at(j).trimmed().toDouble(&ok);
                if (!ok)return false;
                //摩擦系数-各向同性
                if (num == 1 && flagAnisotropic == 1)
                    behavior->setFrictionCoeff(v, index);
                //摩擦系数1-各向异性
                else if (num == 1 && flagAnisotropic == 2)
                    behavior->setFrictioinCoeff1(v, index);
                //摩擦系数2-各向异性
                else if (num == 2 && flagAnisotropic == 2)
                    behavior->setFrictionCoeff2(v, index);
                //滑移速率
                else if (num == flagAnisotropic + 1)
                    behavior->setSlipRate(v, index);
                //接触压力
                else if (num == flagAnisotropic + 2)
                    behavior->setContactPressure(v, index);
                //温度
                else if (num == flagAnisotropic + 3)
                    behavior->setTemperature(v, index);
            }
            if (num % 8 != 0 || num / 8 == row)
            {
                ++index;
                if (num < flagAnisotropic)return false;
                num = 0;
            }
        }
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactTangentialBehavior::writeContactTangentialBehaviorToFrictionless()
    {
        if (!_iteractionPropContact || !_writer)return false;
        //获取切向行为
        Interface::FITKAbaInteractionContactTangentialBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactTangentialBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::TangentialBehavior));
        if (!behavior)return false;
        QTextStream* stream = _writer->getStream();
        *stream << "*Friction" << endl;
        *stream << "0.," << endl;
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactTangentialBehavior::writeContactTangentialBehaviorToPenalty()
    {
        if (!_iteractionPropContact || !_writer)return false;
        //获取切向行为
        Interface::FITKAbaInteractionContactTangentialBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactTangentialBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::TangentialBehavior));
        if (!behavior)return false;
        QTextStream* stream = _writer->getStream();
        //写出Friction
        QString lineOne = "*Friction";
        //写出弹性滑动表面尺寸百分比，默认0.005
        if (_isStandard)
            lineOne += ", slip tolerance=0.005";
        //写出是否选择各项异性
        if (behavior->getFrictionDirectionality() == Interface::FrictionDiretionalityEnum::FrictionDiretionality::Anisotropic && _isStandard)
            lineOne += ", anisotropic";
        *stream << lineOne << endl;
        //循环写出行数据
        int count = behavior->getRowCount();
        for (int index = 0; index < count; ++index)
        {
            lineOne.clear();
            if (behavior->getFrictionDirectionality() == Interface::FrictionDiretionalityEnum::FrictionDiretionality::Anisotropic)
                //写出摩擦系数
                lineOne = QString("%1,%1").arg(_writer->double2String(behavior->getFrictionCoeff1(index), 9, 0)).arg(_writer->double2String(behavior->getFrictionCoeff2(index), 9, 0));
            else
                lineOne = _writer->double2String(behavior->getFrictionCoeff(index), 9, 0);
            //写出滑移
            QString slipRate = " ";
            if (behavior->getUseSlipRateDependentData())
                slipRate = _writer->double2String(behavior->getSlipRate(index), 9, 0);
            //写出压力
            QString pressure = " ";
            if (behavior->getUseContactPressureDependentData())
                pressure = _writer->double2String(behavior->getContactPressure(index), 9, 0);
            //写出温度
            QString temperature = " ";
            if (behavior->getUseTemperatureDependentData())
                temperature = _writer->double2String(behavior->getTemperature(index), 9, 0);
            //写出场
            QString field = " ";

            //根据数据类型进行写出
            if (field != " ")
                *stream << QString("%1,%2,%3,%4,%5").arg(lineOne).arg(slipRate).arg(pressure).arg(temperature).arg(field) << endl;
            if (temperature != " ")
                *stream << QString("%1,%2,%3,%4").arg(lineOne).arg(slipRate).arg(pressure).arg(temperature) << endl;
            else if (pressure != " ")
                *stream << QString("%1,%2,%3").arg(lineOne).arg(slipRate).arg(pressure) << endl;
            else if (slipRate != " ")
                *stream << QString("%1,%2").arg(lineOne).arg(slipRate) << endl;
            else
            {
                if (behavior->getFrictionDirectionality() == Interface::FrictionDiretionalityEnum::FrictionDiretionality::Isotropic)
                    lineOne += ",";
                *stream << lineOne << endl;
            }
        }
        return true;
    }
}

//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorInteractionContactNormalBehavior::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorInteractionContactNormalBehavior";
    }

    void FITKAbaqusAdaptorInteractionContactNormalBehavior::setExplicitStandard(int isexplicit, int isstandard)
    {
        _isExplicit = isexplicit;
        _isStandard = isstandard;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::adaptR()
    {
        // 读取
        _iteractionPropContact = dynamic_cast<Interface::FITKAbaInteractionPropertyContact*>(_dataObj);
        if (!_iteractionPropContact || !_reader) return false;
        //类型
        Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType behaviorType =
            Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior;
        bool readOK = false;
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*surface behavior")) return false;
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        //创建数据并获取数据
        _iteractionPropContact->addBehavior(behaviorType);
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(behaviorType));
        if (!behavior)return false;
        //是否允许接触后分离
        if (s.hasKey("no separation"))
            behavior->setAllowSeparationAfterContact(false);
        //获取压力过盈类型
        QString pressureOverclosure = s["pressure-overclosure"].toUpper();
        //"硬"接触
        if (pressureOverclosure == "HARD")
            readOK = this->readContactNormalBehaviorToHardContact();
        //指数
        else if (pressureOverclosure == "EXPONENTIAL")
            readOK = this->readContactNormalBehaviorToExponential();
        //线性
        else if (pressureOverclosure == "LINEAR")
            readOK = this->readContactNormalBehaviorToLinear();
        //表
        else if (pressureOverclosure == "TABULAR")
            readOK = this->readContactNormalBehaviorToTabular();
        //标量因子 Explicit
        else if (pressureOverclosure == "SCALE FACTOR")
            readOK = this->readContactNormalBehaviorToScaleFactor_GeneralContactExplicit();
        if (!readOK)
            _iteractionPropContact->removeBehavior(behaviorType);
        return readOK;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::adaptW()
    {
        // 读取
        _iteractionPropContact = dynamic_cast<Interface::FITKAbaInteractionPropertyContact*>(_dataObj);
        if (!_iteractionPropContact || !_writer) return false;
        //获取切向属性行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return true;
        bool writeOK = false;
        //获取压力过盈类型
        Interface::FITKAbaInteractionContactEnum::PressureOverclosure type = behavior->getPressureOverclosure();
        //硬"接触
        if (type == Interface::FITKAbaInteractionContactEnum::PressureOverclosure::HardContact)
            writeOK = this->writeContactNormalBehaviorToHardContact();
        //指数
        else if (type == Interface::FITKAbaInteractionContactEnum::PressureOverclosure::Exponential)
            writeOK = this->writeContactNormalBehaviorToExponential();
        //线性
        else if (type == Interface::FITKAbaInteractionContactEnum::PressureOverclosure::Linear)
            writeOK = this->writeContactNormalBehaviorToLinear();
        //表
        else if (type == Interface::FITKAbaInteractionContactEnum::PressureOverclosure::Tabular)
            writeOK = this->writeContactNormalBehaviorToTabular();
        //标量因子 Explicit
        else if (type == Interface::FITKAbaInteractionContactEnum::PressureOverclosure::ScaleFactor_GeneralContactExplicit)
            writeOK = this->writeContactNormalBehaviorToScaleFactor_GeneralContactExplicit();
        return writeOK;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::readContactNormalBehaviorToHardContact()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return false;
        //设置过盈类型-"硬"接触
        behavior->setPressureOverclosure(Interface::FITKAbaInteractionContactEnum::PressureOverclosure::HardContact);
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        //关键字处理
        InpKeyLineSpliter s(line, _reader);

        //设置约束执行方法-增广
        if (s.hasKey("augmented Lagrange"))
        {
            behavior->setConstraintEnforcementMethod(Interface::FITKAbaInteractionContactEnum::ConstraintEnformentMethod::AugmentedLagrange_Standard);
        }
        //设置约束执行方法-罚
        else if (s.hasKey("penalty"))
        {
            behavior->setConstraintEnforcementMethod(Interface::FITKAbaInteractionContactEnum::ConstraintEnformentMethod::Penalty_Standard);
        }
        //设置约束执行方法-直接
        else if (s.hasKey("direct"))
        {
            behavior->setConstraintEnforcementMethod(Interface::FITKAbaInteractionContactEnum::ConstraintEnformentMethod::Direct_Standard);
        }
        _reader->readLine();
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::readContactNormalBehaviorToExponential()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return false;
        //设置过盈类型-指数
        behavior->setPressureOverclosure(Interface::FITKAbaInteractionContactEnum::PressureOverclosure::Exponential);
        //获取间隙0的压力与压力0的间隙
        QString line = _reader->readLine().trimmed();
        QStringList str = line.split(',');
        if (str.size() != 2)return false;
        bool ok = false;
        //压力0的间隙
        behavior->setClearanceWhenPressure0(str.at(0).trimmed().toDouble(&ok));
        if (!ok)return false;
        //间隙0的压力
        behavior->setPressureWhenClearance0(str.at(1).trimmed().toDouble(&ok));
        if (!ok)return false;
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::readContactNormalBehaviorToLinear()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return false;
        //设置过盈类型-线性
        behavior->setPressureOverclosure(Interface::FITKAbaInteractionContactEnum::PressureOverclosure::Linear);
        _reader->readLine();
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::readContactNormalBehaviorToTabular()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return false;
        //设置过盈类型-表
        behavior->setPressureOverclosure(Interface::FITKAbaInteractionContactEnum::PressureOverclosure::Tabular);
        _reader->readLine();
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::readContactNormalBehaviorToScaleFactor_GeneralContactExplicit()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return false;
        //设置过盈类型-标量因子 Explicit
        behavior->setPressureOverclosure(Interface::FITKAbaInteractionContactEnum::PressureOverclosure::ScaleFactor_GeneralContactExplicit);
        _reader->readLine();
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::writeContactNormalBehaviorToHardContact()
    {
        if (!_iteractionPropContact || !_writer) return false;
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return false;
        QTextStream* stream = _writer->getStream();
        //写出关键字Surface Behavior
        QString lineOne = "*Surface Behavior";
        //是否允许接触后分离
        if (!behavior->getAllowSeparationAfterContact())
            lineOne += ", no separation";
        //约束执行方法
        Interface::FITKAbaInteractionContactEnum::ConstraintEnformentMethod method = behavior->getConstraintEnforcementMethod();
        //约束执行-默认方法
        if (method == Interface::FITKAbaInteractionContactEnum::ConstraintEnformentMethod::Default)
            *stream << lineOne << ", pressure-overclosure=HARD" << endl;
        else
            return false;
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::writeContactNormalBehaviorToExponential()
    {
        if (!_iteractionPropContact || !_writer) return false;
        //获取法向行为
        Interface::FITKAbaInteractionContactNormalBehavior* behavior = dynamic_cast<Interface::FITKAbaInteractionContactNormalBehavior*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::NormalHehavior));
        if (!behavior)return false;
        QTextStream* stream = _writer->getStream();
        //写出关键字Surface Behavior
        QString lineOne = "*Surface Behavior, pressure-overclosure=EXPONENTIAL";
        *stream << lineOne << endl;
        *stream << QString("%1,%2").arg(_writer->double2String(behavior->getClearanceWhenPressure0(), 8, 0)).arg(_writer->double2String(behavior->getPressureWhenClearance0(), 8, 0)) << endl;
        return true;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::writeContactNormalBehaviorToLinear()
    {
        return false;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::writeContactNormalBehaviorToTabular()
    {
        return false;
    }

    bool FITKAbaqusAdaptorInteractionContactNormalBehavior::writeContactNormalBehaviorToScaleFactor_GeneralContactExplicit()
    {
        return false;
    }
}

//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorInteractionPropertySurfaceContact::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorInteractionPropertySurfaceContact";
    }

    void FITKAbaqusAdaptorInteractionPropertySurfaceContact::setCaseData(AbaqusData::FITKDataCase* caseData)
    {
        _caseData = caseData;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::adaptR()
    {
        //读取
        _iteractionPropContact = dynamic_cast<Interface::FITKAbaInteractionPropertyContact*>(_dataObj);
        if (!_iteractionPropContact || !_reader || !_caseData) return false;
        bool readOK = false;
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*surface interaction")) return false;
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        _iteractionPropContact->setDataObjectName(s["name"]);
        //读取几何属性
        if (!this->readContactGeometricProperty())return false;
        _reader->backLine();
        //读取其他属性行为
        while (!_reader->atEnd())
        {
            readOK = false;
            line = _reader->readLine();
            if (!line.startsWith("*")) continue;
            s.reset(line);
            QString sH = s.getHeader().toLower();
            //发送计算进度信号
            _reader->sendCurrentPercent();
            //读取切向行为
            if (sH == "friction")
                readOK = this->readContactTangentialBehavior();
            //读取法向行为
            else if (sH == "surface behavior")
                readOK = this->readContactNormalBehavior();
            //读取热传导
            else if (sH == "gap conductance")
                readOK = this->readContactThermalConduction();
            else if (line.startsWith("*")) return true;
            if (readOK)
                _reader->backLine();
            else
                break;
        }
        return readOK;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::adaptW()
    {
        //写出
        _iteractionPropContact = dynamic_cast<Interface::FITKAbaInteractionPropertyContact*>(_dataObj);
        if (!_iteractionPropContact || !_writer || !_caseData) return false;
        bool writerOK = true;
        //获取stepManager
        auto stepManager = _caseData->getStepManager();
        if (!stepManager)return false;
        //获取step数量用于下面的遍历
        int count = stepManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            auto step = dynamic_cast<Interface::FITKAbaAbstractStep*>(stepManager->getDataByIndex(i));
            if (!step)continue;
            //判断step是否存在显式
            Interface::FITKAbaAbstractStep::FITKAbaStepType type = step->getStepType();
            if (type == Interface::FITKAbaAbstractStep::FITKAbaStepType::DynamicExplicit)
                _isExplicit = true;
            if (type == Interface::FITKAbaAbstractStep::FITKAbaStepType::GeneralStatic ||
                type == Interface::FITKAbaAbstractStep::FITKAbaStepType::Frequency)
                _isStandard = true;
        }
        //写出函数主体
        writerOK &= writeContactGeometricProperty();
        writerOK &= writeContactTangentialBehavior();
        writerOK &= writeContactNormalBehavior();
        writerOK &= writeContactThermalConduction();
        return  writerOK;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::readContactGeometricProperty()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        bool ok = true;
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        //获取界面层的厚度-Explicit
        double padthickness = 0.0;
        bool isPadthickness = false;
        if (s.hasKey("pad thickness"))
        {
            padthickness = s["pad thickness"].toDouble(&ok);
            if (!ok)return false;
            _iteractionPropContact->addBehavior(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::GeometricProperty);
            isPadthickness = true;
        }
        line = _reader->readLine();
        //获取平面外的表面厚度或截面面积-Standard
        double value = 1.0;
        if (!line.startsWith('*'))
        {
            QStringList str = line.split(',');
            if (str.isEmpty())return false;
            value = str.at(0).toDouble(&ok);
            if (!ok)return false;
            _iteractionPropContact->addBehavior(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::GeometricProperty);
        }
        if (!_iteractionPropContact->hasBehavior(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::GeometricProperty))
            return true;
        //获取几何属性行为数据
        Interface::FITKAbaInteractionContactGeometricProperty* geoPro = dynamic_cast<Interface::FITKAbaInteractionContactGeometricProperty*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::GeometricProperty));
        if (!geoPro)return false;
        //设置参数
        geoPro->setThicknessOfInterLayerValue(padthickness);
        geoPro->setSurfaceThicknessOrCrossSecArea(value);
        geoPro->enableThicknessOfInterLayer(isPadthickness);
        return true;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::readContactTangentialBehavior()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInteractionContactTangentialBehavior>("INP", "Interface::FITKAbaInteractionContactTangentialBehavior");
        if (adaptor == nullptr)
        {
            return false;
        }
        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(_iteractionPropContact);
        bool ok = adaptor->adaptR();

        delete adaptor;
        return ok;
    }
    
    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::readContactNormalBehavior()
    {
        if (!_iteractionPropContact || !_reader) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInteractionContactNormalBehavior>("INP", "Interface::FITKAbaInteractionContactNormalBehavior");
        if (adaptor == nullptr)
        {
            return false;
        }
        //设置读取器
        adaptor->setFileReader(_reader);
        adaptor->setDataObject(_iteractionPropContact);
        bool ok = adaptor->adaptR();

        delete adaptor;
        return ok;
    }
    
    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::readContactThermalConduction()
    {
        return false;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::writeContactGeometricProperty()
    {
        if (!_iteractionPropContact || !_writer || !_caseData)return false;
        QTextStream* stream = _writer->getStream();
        //写出Surface Interaction
        QString lineOne = "*Surface Interaction";
        QString iterPropName = _iteractionPropContact->getDataObjectName();
        iterPropName = iterPropName.contains(' ') ? QString("\"%1\"").arg(iterPropName) : iterPropName;
        lineOne += QString(", name=%1").arg(iterPropName);
        //获取几何属性行为
        Interface::FITKAbaInteractionContactGeometricProperty* behavior = dynamic_cast<Interface::FITKAbaInteractionContactGeometricProperty*>
            (_iteractionPropContact->getBehavoir(Interface::FITKAbaInteractionContactBehaviorEnum::FITKAbaContactBehaviorType::GeometricProperty));
        if (!behavior)
        {
            *stream << lineOne << endl;
            if (!_isExplicit)
                *stream << QString("%1,").arg("1.") << endl;
            return true;
        }
        if (behavior->isEnableThicknessOfInterLayer())
            lineOne += QString(", pad thickness=%1").arg(_writer->double2String(behavior->getThicknessOfInterLayerValue(), 8, 0));
        *stream << lineOne << endl;
        if (!_isExplicit)
            *stream << QString("%1,").arg(_writer->double2String(behavior->getSurfaceThicknessOrCrossSecArea(), 8, 0)) << endl;
        return true;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::writeContactTangentialBehavior()
    {
        if (!_iteractionPropContact || !_writer) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInteractionContactTangentialBehavior>("INP", "Interface::FITKAbaInteractionContactTangentialBehavior");
        if (adaptor == nullptr)
        {
            return false;
        }
        //设置读取器
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(_iteractionPropContact);
        adaptor->setExplicitStandard(_isExplicit, _isStandard);
        bool ok = adaptor->adaptW();

        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::writeContactNormalBehavior()
    {
        if (!_iteractionPropContact || !_writer) return false;
        //通过工厂创建
        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKAbaqusAdaptorInteractionContactNormalBehavior>("INP", "Interface::FITKAbaInteractionContactNormalBehavior");
        if (adaptor == nullptr)
        {
            return false;
        }
        //设置读取器
        adaptor->setFileWriter(_writer);
        adaptor->setDataObject(_iteractionPropContact);
        adaptor->setExplicitStandard(_isExplicit, _isStandard);
        bool ok = adaptor->adaptW();

        delete adaptor;
        return ok;
    }

    bool FITKAbaqusAdaptorInteractionPropertySurfaceContact::writeContactThermalConduction()
    {
        return true;
    }
}
